package com.up.vms.application.common.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * @author ZhangSuchao
 * @create 2019/1/12
 * @since 1.0.0
 */
public class DateUtil {
    public final static String formatYearMonthDay = "yyyy-MM-dd hh:mm:ss";

    public static String fomatDateToString(String strDate) throws ParseException {
        if (StringUtils.isEmpty(strDate)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formatYearMonthDay);
        return sdf.format(sdf.parse(strDate));
    }

    public static Date formatDateFromString(String strDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(formatYearMonthDay);
        return sdf.parse(strDate);
    }

    public static String dateFomat(Date date) {
        if (Objects.isNull(date)) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(formatYearMonthDay);
        return sdf.format(date);
    }

    public static String dateFomatYYMMDD(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMdd");
        return sdf.format(date);
    }

    public static String dateFomatYYYYMMDD(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return sdf.format(date);
    }

    public static String dateFomatYYMMDDHHMMSS(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    public static String stampFormatYYMMDDHH(String time) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long lt = new Long(time);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

    public static Date stampToDate(String s) {
        if ("".equals(s) || null == s) {
            return null;
        }
        long lt = new Long(s);
        return new Date(lt);
    }

    /*******
     * 任意string转date
     * @param dateString
     * @return Date
     */
    public static Date string2Date(String dateString, String type) throws ParseException {
        if (StringUtils.isEmpty(dateString)) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(type);
        return sdf.parse(dateString);
    }

    public static Date stampToDate1(String s) {
        if ("".equals(s)) {
            return null;
        }
        long lt = new Long(s);
        return new Date(lt * 1000);
    }

    public static Date msecStampToDate(long s) {
        if (s == 0L) {
            return null;
        }
        return new Date(s * 1000);
    }

    public static String timeDispose(String strDate) {
        String retStringTime = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (!"".equals(strDate)) {
            retStringTime = simpleDateFormat.format(DateUtil.stampToDate(strDate));
        }
        return retStringTime;
    }

    public static String timeDisposeYYYYMMDD(String strDate) {
        String retStringTime = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (!"".equals(strDate)) {
            retStringTime = simpleDateFormat.format(DateUtil.stampToDate(strDate));
        }
        return retStringTime;
    }

    public static String timeDisposeToDay(String strDate) {
        String retStringTime = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (!StringUtils.isEmpty(strDate)) {
            retStringTime = simpleDateFormat.format(DateUtil.stampToDate(strDate));
        }
        return retStringTime;
    }

    public static String getDateTimeFromStamp(String stamp) {
        if (StringUtils.isEmpty(stamp)) {
            return null;
        }
        Date date = stampToDate(stamp);
        return dateFomat(date);
    }

    public static Date getCurrentYMD() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return format.parse(LocalDate.now().toString());
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static Date getDateYM(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        try {
            return format.parse(date.toString());
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static Date getCurrent() {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneId zoneId = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zoneId).toInstant();
        return Date.from(instant);
    }

    /**
     * 获取制定一天的 前面的第几天或者之后的第几天
     *
     * @param oneDay
     * @param dayNum
     * @return
     */
    public static Date getOneDayBeforeOrAfter(String oneDay, int dayNum) {
        if (Objects.isNull(oneDay) || "".equals(oneDay)) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Long time = Long.valueOf(oneDay);
        String day1 = simpleDateFormat.format(time);
        Date date = null;
        try {
            date = simpleDateFormat.parse(day1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int day = calendar.get(Calendar.DATE);
        calendar.set(Calendar.DATE, day + dayNum);
        return calendar.getTime();

    }

    public static Date parse(String date) {
        return parse(date, formatYearMonthDay);
    }

    public static Date parse(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static Date getLastDayOfMonth(int year, int month) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, lastDay);
        calendar.set(year, month - 1, lastDay, 23, 59, 59);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(sdf.format(calendar.getTime()));
    }

    public static Date getFirstDayOfMonth(int year, int month) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        int firstDay = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, firstDay);
        SimpleDateFormat sdf = new SimpleDateFormat(formatYearMonthDay);
        return sdf.parse(sdf.format(calendar.getTime()));
    }

    //获取时间戳
    public static String timeStampString() {
        long time = System.currentTimeMillis();
        String t = String.valueOf(time / 1000);
        return t;
    }

    /**
     * 当天开始时间
     *
     * @return
     */
    public static Date getIntradayStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 当天结束时间
     *
     * @return
     */
    public static Date getIntradayEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 999);
        return todayEnd.getTime();
    }

    public static Date formatDateFromStringYyyyMMddHHmmss(String strDate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.parse(strDate);
    }

}
